package com.taobao.dao.impl;

import java.util.Iterator;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.taobao.dao.CommonDao;
import com.taobao.dao.UserInfoDao;
import com.taobao.pojo.Config;
import com.taobao.pojo.HttpContext;
import com.taobao.pojo.UserInfo;

public class UserInfoDaoImpl extends CommonDao implements UserInfoDao {

    @Override
    public Object process(HttpContext context) throws Exception {
        UserInfo userInfo = new UserInfo();
        Document doc = Jsoup.parse(context.getHtml());

        String userName = doc.getElementById("UserNameLbl").text().trim();
        userInfo.setUserName(userName);

        String vip = doc.getElementById("UserNameLbl").child(0).child(0).attr("alt").trim();
        userInfo.setVip(vip);

        Elements eles = doc.getElementById("TiXing_Price_Lbl").getElementsByAttributeValue("class", "price");
        Iterator<Element> iterator = eles.iterator();
        userInfo.setUserName(Config.userName);
        userInfo.setMoney(iterator.next().text().trim());
        userInfo.setpPoint(iterator.next().text().trim());
        userInfo.setIntegral(iterator.next().text().trim());
        userInfo.setWarning(iterator.next().text().trim());

        return userInfo;
    }

    @Override
    public UserInfo getUserInfo() throws Exception {
        return (UserInfo) getRequest(Config.url + "user.aspx");
    }
}
